/* created by Rui Lopes */

openwebpt.mctes.fct.phd.visualize = function() {
	
/*

the actual query is

select * from html where url="http://spreadsheets.google.com/pub?key=tCN7bcqKAgVELdd5me29UjA&single=true&gid=0&output=html"
and xpath="//table[@id='tblMain_0']/tr[position()>4 and position()<=18]"

*/
	
$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fspreadsheets.google.com%2Fpub%3Fkey%3DtCN7bcqKAgVELdd5me29UjA%26single%3Dtrue%26gid%3D0%26output%3Dhtml%22%20and%20xpath%3D%22%2F%2Ftable%5B%40id%3D'tblMain_0'%5D%2Ftr%5Bposition()%3E%3D4%20and%20position()%3C%3D18%5D%22&format=json&callback=?",

	function (data) {
		var results = data.query.results;
		var dt = new google.visualization.DataTable();
		
		//console.log(results);
		
		var num_areas = 7;
		var num_anos  = 14;
		
		dt.addRows(num_areas * num_anos);
		dt.addColumn('string', "PhD");
		dt.addColumn('number', "Ano de candidatura");
		dt.addColumn('number', "Bolsas");
		dt.addColumn('string', "Area");
		
		var inc = 0;
		
		for (var i = 0; i < num_areas; i++) {
			var area = results.tr[0].td[i+2].p;
			
			for (var j = 0; j < num_anos; j++) {
				var ano = parseInt(results.tr[j+1].td[1].p);
				var bolsas = parseInt(results.tr[j+1].td[i+2].p);
				
				dt.setValue(inc, 0, area);
				dt.setValue(inc, 1, ano);
				dt.setValue(inc, 2, bolsas);
				dt.setValue(inc, 3, area);
				
				inc++;
			}
		}
		
		var chart = new google.visualization.MotionChart(document.getElementById('chart'));
		chart.draw(dt, {});
	});				
};

google.load("jquery", "1");
google.load('visualization', '1', {'packages':['motionchart']});
google.setOnLoadCallback(openwebpt.mctes.fct.phd.visualize);